import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;


public class Main {
public static boolean UNKNOWNKEY = false;
public static boolean MULTITHREAD = false;
	public static void main(String[] args) {
		if(UNKNOWNKEY)
		{
			if(MULTITHREAD)
			{
				DESthread DES0 = new DESthread(0,0,0);
				Thread t0 = new Thread( DES0 );
				t0.start();
				DESthread DES1 = new DESthread(0,1,1);
				Thread t1 = new Thread( DES1 );
				t1.start();
				DESthread DES2 = new DESthread(1,0,2);
				 Thread t2 = new Thread( DES2 );
				 t2.start();
			}
			DESthread DES3 = new DESthread(1,1,3);
	        Thread t3 = new Thread( DES3 );
	        t3.start();
		}
        else
        {
        	// DECRYPTED KEY 11100011 01000110 10111010 00101001 11100000 01000101 10111010 00101010
        	int [] decryptkey = {1,1,1,0,0,0,1,1, 0,1,0,0,0,1,1,0, 1,0,1,1,1,0,1,0, 0,0,1,0,1,0,0,1, 1,1,1,0,0,0,0,0, 0,1,0,0,0,1,0,1, 1,0,1,1,1,0,1,0, 0,0,1,0,1,0,1,0};
        	String ciphertext = "jogjQrIyHp4 " +
        	"fs/QHLTaWDk " +
        	"fx7gH2/AHVo " +
        	"fnHRuEXfcjE " +
        	"nX6d4GAdmhE " +
        	"oigoQG04V0g " +
        	"O7oHHrxPk9Q " +
        	"D0yoBsmuUE8 " +
        	"APdaeAc29nU " +
        	"BdrhzTRKzBA " +
        	"WBOoaZWCPxE " +
        	"JYf3KZgQ1Qc " +
        	"k7ZM9RAzA5k " +
        	"+5YzAlBOXL8 " +
        	"HwgYGg74e4E " +
        	"hWo+tcRX22s " +
        	"R3wHM+fQzh4 " +
        	"QL5EZ0pRmjs " +
        	"WfuxLk2k/YQ " +
        	"MrE4OEFCoVs " +
        	"cos8wa3njQA " +
        	"datcs3lQXyI " +
        	"J/WYz9Qxvgo " +
        	"QyH8Vu504+A " +
        	"vfabGSMprcc " +
        	"pY0C8J9g7ls " +
        	"Iw2W3pJzVv0 " +
        	"FXQTxUnFgGg " +
        	"nUIHvBKNnjE " +
        	"9g8kOg30gDQ " +
        	"bZmjwH5fwSI " +
        	"wcibMp401+o " +
        	"4jo/biH/nFQ " +
        	"emY+FlAusNg " +
        	"rWmP0F65Ttg " +
        	"/WIOFkSny9w " +
        	"odNxf1tNEJU " +
        	"WJv+xSmdyZo " +
        	"CNLsIVduQNc " +
        	"QbsgBE9HfSY";
        	Scanner fin = new Scanner(ciphertext);
        	FileWriter fout;
        	DES code;
        	try {
				fout = new FileWriter(new File("DecryptedText.txt"));
				code = new DES(decryptkey, true);
				
				code.setKeys();
				code.reverseKeys();
	        	// Read in each block and process...
				while (fin.hasNext()) {
					String myblock = fin.next();
					if (myblock == null) break;
					int[] binblock = longrad64ToBinary(myblock);
					//System.out.println("Block: " + myblock);
					code.setBlock(binblock);
					code.encrypt();
					code.print(fout);
				}
        	} catch (Exception e) {
				e.printStackTrace();
			}
        }
        	
	}
	public static int[] longrad64ToBinary(String str)
	{
		int[] bin = new int[64];
		int val = 0;
		for(int i = 0; i<10; i++)
		{
			char ch = str.charAt(i);
			if(ch >= 'A' && ch <= 'Z')
				val = ch - 'A';
			else if(ch >= 'a' && ch <= 'z')
				val = ch - 'a' + 26;
			else if(ch >= '0' && ch <= '9')
				val = ch - '0' + 52;
			else if(ch == '+')
				val = 62;
			else if(ch == '/')
				val = 63;
			else
			{
				System.out.println("Conversion error");
				System.exit(0);
			}
			for (int j=0; j<6; j++)
			{
				bin[6*i+5-j]=val%2;
				val /= 2;
			}
		}
		char ch = str.charAt(10);
		if(ch >= 'A' && ch <= 'Z')
			val = ch - 'A';
		else if(ch >= 'a' && ch <= 'z')
			val = ch - 'a' + 26;
		else if(ch >= '0' && ch <= '9')
			val = ch - '0' + 52;
		else if(ch == '+')
			val = 62;
		else if(ch == '/')
			val = 63;
		else
		{
			System.out.println("Conversion error");
			System.exit(0);
		}
		val/=4;
		for (int j=0; j<4; j++)
		{
			bin[63-j]=val%2;
			val /= 2;
		}
		return bin;
	}
	
	// incoming is 64 bit binary array
	public static String shortbinaryToRad64(int[] bin)
	{
		String rad64 = "";
		int val = 0;
		String temp = "";
		for(int i = 0; i<10; i++)
		{
			temp = "";
			for(int j = 0; j<6; j++)
			{
				temp += bin[6*i + j];
			}
			val = Integer.parseInt(temp, 2);
			char ch = 0;
			if(val >= 0 && val <= 25)
				ch = (char) (val + 'A');
			else if(val >= 26 && val <= 51)
				ch = (char) (val + 'a' - 26);
			else if(val >= 52 && val <= 61)
				ch = (char) (val + '0' - 52);
			else if(val == 62)
				ch = '+';
			else if(val == 63)
				ch = '/';
			else
			{
				System.out.println("Conversion error");
				System.exit(0);
			}
			rad64 = rad64 + ch;
		}
		return rad64;
	}

}
